package com.discuz.spider.jpkoo;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;

import org.apache.commons.io.FileUtils;

import com.discuz.spider.WebClientAgent;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;

public class JPKooSpider {
	
	public static String STORE_DIR="/Users/only/share/jpkoo/";
public static String domain = "http://www.jpkoo.com/";
	public static void main(String[] args) throws MalformedURLException,
			IOException, Throwable {
		
		
	}
	
	public static void getAllPostURL() throws IOException{
		FileUtils.writeStringToFile(new File(STORE_DIR+"siwameitui_postlist.txt"), "");
		
		 for (int q = 1; q < 138; q++) {
			String page_no = "http://www.jpkoo.com/forum-59-" + q + ".html";
			
			
		   String c = getPostList(page_no);
		   FileUtils.writeStringToFile(new File(STORE_DIR+"postlist.txt"),c,true);
		   
		   System.out.println(q);
		 }
	}
	
	public static void loadAllPost() throws IOException  {
		
		List<String> all = FileUtils.readLines(new File("/Users/only/share/1.txt"));
		int index =1;
		for(String f : all){
			index++;
			try {
				if(index<=964)continue;
			
			String [] e = f.split("\\|");
			if(e.length ==3){
				System.out.println(index +"-"+e[1]);
				FileUtils.writeStringToFile(new File(STORE_DIR+index+"/1.txt"), e[0]+"|"+e[2]);
				int count = parserPost(e[1],index);
				if(count==0){
					FileUtils.writeStringToFile(new File("/Users/only/share/no_image.txt"), f, true);
				}
				System.out.println("-----------------------------------------------------------------");
			}else{
				System.out.println(index +"------>eroor");
				break;
			}
			
		
		} catch (IOException e) {
			e.printStackTrace();
			break;
		} catch (Exception e) {
			e.printStackTrace();
			break;
		}
		}
		
	}

	public static String getPostList(String u) {

		WebClient client = WebClientAgent.get();
		StringBuffer bf = new StringBuffer();
		try {
			HtmlPage p = client.getPage(u);
			@SuppressWarnings("unchecked")
			List<HtmlAnchor> ls = (List<HtmlAnchor>) p
					.getByXPath("//*[@class='xst']");
			for (HtmlAnchor ha : ls) {
				bf.append(ha.asText().trim());
				bf.append('|');
				bf.append(ha.getAttribute("href"));
				bf.append('\n');
			
			}
		} catch (FailingHttpStatusCodeException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String rs =  bf.toString();
		bf =null;
		return rs;
	}

	public static int parserPost(String url,int index) throws Exception {
		WebClient client = WebClientAgent.get();
int count=0;
		try {
			
			HtmlPage postPage = client.getPage(url);
			
			HtmlAnchor reply = (HtmlAnchor) postPage
					.getFirstByXPath("//*[@class='locked']/a[1]");

			//must reply
			if (reply != null) {
				System.out.println("must reply post....");
				
				Thread.sleep(5000);
				HtmlPage rp = reply.click();

				HtmlTextArea text = rp.getFirstByXPath("id('e_textarea')");
				if(text == null){
					text = rp.getFirstByXPath("id('postmessage')");
					HtmlButton btn = rp.getFirstByXPath("id('postsubmit')");

					if(text!=null && btn!=null)
					{
						text.blur();
						text.setText(Utils.randomReply());
	
						btn.click();
						Thread.sleep(1000);
						postPage = client.getPage(url);
					}
				}
			
			}else{
				System.out.println("not must reply post");
			}
			//not must reply
			//parser image 
			
			
			@SuppressWarnings("unchecked")
			List<HtmlImage> ls = (List<HtmlImage>) postPage
					.getByXPath("//*[@inpost='1']");
			int k =1;
			if(ls == null || ls.size() ==0){
				 ls = (List<HtmlImage>) postPage
						.getByXPath("//img[@style='cursor: pointer']");
			}
			
			if(ls != null){
			System.out.println("parser image atts size :" +ls.size());
			
			for(HtmlImage hi:ls){
				if(k>20)break;//max value 15
				String u = domain +hi.getAttribute("file");
				String ext_name = Utils.checkImageName(u);
				String f = STORE_DIR+index+"/so"+k+"."+ext_name;
				System.out.println(u);
				byte [] data = Utils.download(u);
				if(data != null){
					FileUtils.writeByteArrayToFile(new File(f), data);
					k++;
				}
				
			}
			}
			System.out.println("save image atts size :" +(k-1));
			count = k-1;

		} catch (FailingHttpStatusCodeException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return count;
	}

	
}

